package com.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.AuthorDao;
import com.dao.BookDao;
import com.dao.LoginDao;
import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.dao.UserDao;
import com.model.manyToMany.Student;
import com.model.manyToMany.Teacher;
import com.model.manyToOne.Author;
import com.model.manyToOne.Book;
import com.model.oneToOne.Login;
import com.model.oneToOne.User;
import com.service.FacadeService;

@Service("facadeService")
public class FacadeServiceImpl implements FacadeService {

	private final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired 
	private AuthorDao authorDao;
	@Autowired 
	private BookDao bookDao;
	
	public Author findAuthorById(Integer id) {
		return authorDao.get(id);
	}

	public void saveAuthor(Author author) {
		authorDao.save(author);
	}

	public Book findBookById(Integer id) {
		return bookDao.get(id);
	}

	public void saveBook(Book book) {
		bookDao.save(book);
	}
	
	public void deleteAuthor(Integer id) {
		authorDao.delete(findAuthorById(id));
	}

	public void deleteBook(Integer id) {
		Book book = findBookById(id);
		book.setAuthor(null);
		
		bookDao.delete(book);
	}
	
	//--------------------------------------------------------




	@Autowired 
	private TeacherDao teacherDao;
	@Autowired 
	private StudentDao studentDao;
	
	public Teacher findTeacherById(Integer id) {
		return teacherDao.get(id);
	}

	public void saveTeacher(Teacher teacher) {
		teacherDao.save(teacher);
	}

	public Student findStudentById(Integer id) {
		return studentDao.get(id);
	}

	public void saveStudent(Student student) {
		studentDao.save(student);
	}

	//删除teacher及中间表
	public void deleteTeacher(Integer id) {
		teacherDao.delete(id);
	}
	
	//删除中间表
	public void deleteTeacher2(Integer id) {
		Teacher teacher = findTeacherById(id);
		teacher.setStudents(null);
		teacherDao.update(teacher);
	}

	//删除student及中间表
	public void deleteStudent(Integer id) {
		Student student = studentDao.get(id);
		
		List<Teacher> teachers = student.getTeachers();
		for (Teacher t : teachers) {
			t.getStudents().remove(student);
		}
		
		studentDao.delete(student);
	}
	
	//删除中间表
	public void deleteStudent2(Integer id) {
		Student student = findStudentById(id);
		
		List<Teacher> teachers = student.getTeachers();
		for (Teacher t : teachers) {
			t.setStudents(null);
		}
		
		studentDao.update(student);
	}
	
	

	@Autowired 
	private UserDao userDao;
	@Autowired 
	private LoginDao loginDao;
	
	public List<User> findUserByDate(String beginDate, String endDate) {
		String query = "FROM User u WHERE u.createDate BETWEEN ? AND ?";
		Date bDate = null;
		Date eDate = null;
		try {
			bDate = dateFormat.parse(beginDate + " 00:00:00");
			eDate = dateFormat.parse(endDate + " 23:59:59");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Object[] params = {bDate, eDate};
		return userDao.find(query, params);
	}

	public void saveUser(User user) {
		userDao.save(user);
	}

	public void saveLogin(Login login) {
		loginDao.save(login);
	}

	public void deleteUser(Integer id) {
		userDao.delete(id);
	}

	public void deleteLogin(Integer id) {
		loginDao.delete(id);
	}
	
	
}
